//
//  BindingPhoneVc.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/1/9.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

class BindingPhoneVc: TTBaseVC, UITextFieldDelegate {
    lazy var phoneField:UITextField = {
        let text = UITextField.init(frame: FRAME_CGFloat(37.5, 200, SCREEN_WIDTH - 75, 50))
        text.placeholder = "请输入手机号"
        text.font = FONT(15)
        let imageView = UIView.init(frame: FRAME_CGFloat(0, 50, SCREEN_WIDTH - 75, 1))
        imageView.backgroundColor = RGB_Same(240, 1)
        text.addSubview(imageView)
        return text
    }()
    
    lazy var codeField:UITextField = {
        let text = UITextField.init(frame: FRAME_CGFloat(37.5, 275, SCREEN_WIDTH - 155, 50))
        text.placeholder = "验证码"
        text.font = FONT(15)
        let imageView = UIView.init(frame: FRAME_CGFloat(0, 50, SCREEN_WIDTH - 75, 1))
        imageView.backgroundColor = RGB_Same(240, 1)
        text.addSubview(imageView)
        return text
    }()
    
    lazy var bindingBtn:UIButton = {
        
        let btn = UIButton.init(type: .custom)
        btn.frame = FRAME_CGFloat(37.5, 350, SCREEN_WIDTH - 75, 44)
        btn.setBackgroundImage(IMAGE(name: "Rectangle"), for: .normal)
        btn.setTitle("绑定", for: .normal)
        btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
        btn.addTarget(self, action: #selector(bindingBtnAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var noBindingBtn:UIButton = {
        
        let btn = UIButton.init(type: .custom)
        btn.frame = FRAME_CGFloat(37.5, 400, SCREEN_WIDTH - 75, 44)
        btn.layer.borderWidth = 1
        btn.layer.borderColor = RGB_Same(240).cgColor
        btn.layer.cornerRadius = 3
        btn.setTitle("暂不绑定", for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        btn.setTitleColor(BlackBackColor, for: .normal)
        btn.addTarget(self, action: #selector(noBindingBtnAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var validateCodeBtn: TTValidButton = {
        let btn = TTValidButton.init(type: .custom)
        btn.frame = FRAME_CGFloat(SCREEN_WIDTH - 137.5, 290, 100                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   , 21)
        btn.addTarget(self, action: #selector(validateCodeBtnAction), for: .touchUpInside)
        return btn
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        SET_LINEVIEW(view: self.view)
        self.navigationItem.title = "绑定手机号"
        setUI()
        prepare()
    }
    

    func setUI() {
        let image = UIImageView.init(frame: FRAME_CGFloat(SCREEN_WIDTH / 2 - 32, 63, 64, 108.5))
        image.image = IMAGE(name: "logoShadow")
        image.layer.cornerRadius = 10
        image.layer.masksToBounds = true
        self.view.addSubview(image)
        self.view.addSubview(phoneField)
        self.view.addSubview(codeField)
        self.view.addSubview(validateCodeBtn)
        self.view.addSubview(bindingBtn)
        self.view.addSubview(noBindingBtn)
        validateCodeBtn.setTitle("获取验证码", for: .normal)
        validateCodeBtn.setTitleColor(BlackBackColor, for: .normal)
        
        self.view.addGestureRecognizer(UITapGestureRecognizer(target:self, action:#selector(handleTap(sender:))))
    }
    
    @objc func handleTap(sender: UITapGestureRecognizer) {
        if sender.state == .ended {
            phoneField.resignFirstResponder()
            codeField.resignFirstResponder()
        }
        sender.cancelsTouchesInView = false
    }

}
//  MARK: - ###### 私有方法 ######
extension BindingPhoneVc {
    @objc func validateCodeBtnAction() {
        if !TTValidator.isTelNumber(num: phoneField.text!) {
            TTAlertView.showMessage(message: "请输入正确的手机号码！")
            return ;
        }
        
        weak var weakSelf = self
        weakSelf?.validateCodeBtn.startTime(60)

        MineRequestModel.LoadVerCode(jointArr: [self.phoneField.text!,String.getUserId()], parameters:["type":"login"]) { (suc, mes) in
            TTAlertView.showMessage(message: mes)
        }
    }
    @objc func bindingBtnAction() {
        MineRequestModel.CheckVerCode(parameters: ["mobile":self.phoneField.text!,"verCode":self.codeField.text!,"userId":String.getUserId()]) { (suc, mes) in
            if suc {
                self.navigationController?.popViewController(animated: true)
                TTAlertView.showMessage(message: "绑定成功")
            } else {
                TTAlertView.showMessage(message: mes)
            }
        }
    }
    @objc func noBindingBtnAction() {
        self.navigationController?.popViewController(animated: true)
    }
    fileprivate func prepare() {
        self.phoneField.addTarget(self, action: #selector(textFieldDidChange), for: UIControl.Event.editingChanged)
        self.codeField.addTarget(self, action: #selector(textFieldDidChange), for: UIControl.Event.editingChanged)
    }
    
    fileprivate func setLoginBtnStatue(isSelected: Bool) {
        if isSelected {
            self.bindingBtn.setTitleColor(WhiteBackColor, for: UIControl.State.normal)
            self.bindingBtn.isUserInteractionEnabled = true
        } else {
            self.bindingBtn.setTitleColor(WhiteBackColor, for: UIControl.State.normal)
            self.bindingBtn.isUserInteractionEnabled = false
        }
    }
    
    @objc fileprivate func textFieldDidChange() {
        
        self.phoneField.text = limitInput(digit: 11, input: phoneField.text!)
        self.codeField.text = limitInput(digit: 6, input: codeField.text!)
        
        if checkInput() {
            setLoginBtnStatue(isSelected: true)
        } else {
            setLoginBtnStatue(isSelected: false)
        }
    }
    
    fileprivate func checkInput() -> Bool {
        if !validMobile(mobile: self.phoneField.text!) {
            return false
        }
        if !validPassword(password: self.codeField.text!) {
            return false
        }
        
        return true
    }
}
